import datetime
from django.db import models
from django.forms import ModelForm
from django import forms


from hashlib import md5

class Conf(models.Model):
    TIPOS = (
        ( 's', 'String (texto)' ),
        ( 'i', 'Integer (numero inteiro)' ),
        ( 'f', 'Float (numero decimal)' ),
        ( 'b', 'Boolean (verdadeiro ou falso)' ),
    )
    chave = models.CharField(max_length=64, unique=True)
    valor = models.CharField(max_length=64)
    tipo  = models.CharField(max_length=1, choices=TIPOS, default='s')


class ConfForm(ModelForm):
    class Meta:
        model = Conf


class Grupo(models.Model):
    nome = models.CharField(max_length=50)
    descricao = models.CharField(max_length=255)
    data = models.DateTimeField(auto_now_add=True)
    
    def __unicode__(self):
        return self.nome
    
    
class GrupoForm(ModelForm):
    class Meta:
        model = Grupo
    



class Usuario(models.Model):

    usuario = models.CharField(max_length=32)
    admin = models.CharField(max_length=1, default='n')
    senha = models.CharField(max_length=32)
    pessoa = models.CharField(max_length=1, default='f')
    nome = models.CharField(max_length=128)
    cpf_cnpj = models.CharField(max_length=32)
    rg_ie = models.CharField(max_length=32)
    endereco = models.CharField(max_length=128)
    bairro = models.CharField(max_length=128)
    cidade = models.CharField(max_length=128)
    estado = models.CharField(max_length=2)
    cep = models.CharField(max_length=10)
    telefone = models.CharField(max_length=14)
    celular = models.CharField(max_length=14)
    email = models.CharField(max_length=64)
    revenda = models.CharField(max_length=1, default='n')
    fantasia = models.CharField(max_length=32, blank=True, null=True)
    logo = models.CharField(max_length=256, blank=True)
    contatos = models.CharField(max_length=128, blank=True, null=True)
    data = models.DateTimeField(default=datetime.datetime.now())
    grupo = models.ForeignKey(Grupo)

    def _get_revenda_id(self):
        return md5(str(self.id)).hexdigest() if self.revenda=='s' else None
    revenda_id = property(_get_revenda_id)

class UsuarioForm(ModelForm):
    class Meta:
        model = Usuario
    

